<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
<!--        <a-button type="primary" @click="handleCreate"> 新增成员 </a-button>-->
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[

              {
                // icon: 'clarity:note-edit-line',
                label:'查看',
                color:'success',
                onClick: handleView.bind(null, record),
              },
              {
                label:'关闭',
                color:'error',
                popConfirm: {
                  title: '是否关闭该订单？',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
              // {
              //   icon: 'ant-design:delete-outlined',
              //   color: 'error',
              //   popConfirm: {
              //     title: '是否确认删除',
              //     placement: 'left',
              //     confirm: handleDelete.bind(null, record),
              //   },
             // },
            ]"
          />
        </template>
        <template v-if="column.dataIndex === 'isReturn'">
          <div style="width: 100%; cursor: pointer" @click="openUserModal(record)">
            <span v-if="record.isReturn == 1" style="color: green">是(点击看明细)</span>
            <span v-else="record.isReturn == 0">否</span>

          </div>
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
    <OrderListModal @register="orderModal[0]" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { ref,onMounted } from 'vue';
  import { useRoute } from 'vue-router';
  import { purchaseOrderTableListApi,priceTableListApi } from '@/api/sys/msUser';
  const route = useRoute();
  import OrderListModal from './OrderListModal.vue';

  import FormModal from './FormModal.vue';
  import { columns, searchFormSchema } from './role.data';
  import {useMessage} from "@/hooks/web/useMessage";
  import {useModal} from "@/components/Modal";


  defineOptions({ name: 'userNoteList' });
  const { createMessage } = useMessage();
  const loading=ref(false);

  const userName = ref(route.params?.userName);

  const [register, { openModal, closeModal }] = useModal();

  const orderModal = useModal();

  const [registerTable, { reload }] = useTable({
    title: '费用明细列表',
    api: priceTableListApi,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 110,
      title: '操作',
      dataIndex: 'action',
      fixed: "right",
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function format(seconds) {//秒转化为分钟格式
    let s=Math.trunc(seconds);
    let minInt=Math.floor(s / 60);
    let min = minInt >= 10 ? minInt : '00' + minInt;
    s -= 60 * min;
    let sec = s >= 10 ? s : '00' + s;
    return  min + ':' + sec;
  }


  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
  function handleView(record: Recordable) {
   let url=record.mp3Url?record.mp3Url:record.remoteUrl;
    // let url=record.remoteUrl;
    window.open(url)
  }

  function handleDelete(record: Recordable) {
    //console.log(record);

    deleteApi(record).then(res => {
      createMessage.success(res.message);
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        reload();
      });
  }

  function handleSuccess() {
    reload();
  }
  const arrHeader=[
    {
     key:"userName",
     value:"姓名"
    },{
      key:"levelName",
      value:"职级"
    }
  ];

  function getHeadArr(){
    let arr=[];
    arrHeader.forEach(r=>{
      arr.push(r.value)
    })
    return arr;
  }

  function changeSubmitArr(results){
    let data={
      list:[]
    }
    let list=[];
    results.forEach(r=>{
      let json={};
      for(let key in r){
        arrHeader.forEach(e=>{
          if(e.value==key){
            json[e.key]=r[key];
          }
        })
      }
      data.list.push(json)
    });
    return data;
  }

  function openUserModal(record: Recordable) {
    orderModal[1].openModal(true, record);
  }


</script>
